package com.github.caijh.mall.front.controller;


import java.util.List;

import com.github.caijh.framework.web.controller.BaseController;
import com.github.caijh.mall.category.vo.CategoryTreeVo;
import com.github.caijh.mall.common.CommonPage;
import com.github.caijh.mall.common.CommonResult;
import com.github.caijh.mall.common.PageParams;
import com.github.caijh.mall.front.request.IndexStoreProductSearchRequest;
import com.github.caijh.mall.front.request.ProductRequest;
import com.github.caijh.mall.front.response.ProductDetailResponse;
import com.github.caijh.mall.front.response.ProductResponse;
import com.github.caijh.mall.front.response.StoreProductReplayCountResponse;
import com.github.caijh.mall.front.service.ProductService;
import com.github.caijh.mall.store.response.StoreProductReplyResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController("ProductController")
@RequestMapping("api/front")
@Api(tags = "商品")
public class ProductController extends BaseController {

    @Autowired
    private ProductService productService;


    /**
     * 为你推荐
     *
     * @author Mr.Zhang
     * @since 2020-06-02
     */
    @ApiOperation(value = "为你推荐")
    @GetMapping(value = "/product/hot")
    public CommonResult<CommonPage<ProductResponse>> getMenuUser(@Validated PageParams pageParams) {
        IndexStoreProductSearchRequest request = new IndexStoreProductSearchRequest();
        request.setIsHot(true);
        return CommonResult.success(productService.getIndexProduct(request, pageParams));
    }

    @ApiOperation(value = "获取分类")
    @GetMapping(value = "/category")
    public CommonResult<List<CategoryTreeVo>> getCategory() {
        return CommonResult.success(productService.getCategory());
    }

    @ApiOperation(value = "商品列表")
    @GetMapping(value = "/products")
    public CommonResult<CommonPage<ProductResponse>> getList(@Validated ProductRequest request, @Validated PageParams pageParams) {
        return CommonResult.success(productService.getList(request, pageParams));
    }

    @ApiOperation(value = "商品详情")
    @GetMapping(value = "/product/detail/{id}")
    public CommonResult<ProductDetailResponse> getDetail(@PathVariable Integer id) {
        ProductDetailResponse product = productService.getProductDetail(id);
        return CommonResult.success(product);
    }

    /**
     * 商品评论列表
     *
     * @author Mr.Zhang
     * @since 2020-06-03
     */
    @ApiOperation(value = "商品评论列表")
    @GetMapping(value = "/reply/list/{id}")
    @ApiImplicitParam(name = "type", value = "评价等级|0=全部,1=好评,2=中评,3=差评", allowableValues = "range[0,1,2,3]")
    public CommonResult<CommonPage<StoreProductReplyResponse>> getReplyList(
            @PathVariable Integer id,
            @RequestParam(value = "type") Integer type,
            @Validated PageParams pageParams) {
        return CommonResult.success(CommonPage.restPage(productService.getReplyList(id, type, pageParams)));
    }

    /**
     * 商品评论数量
     *
     * @author Mr.Zhang
     * @since 2020-06-03
     */
    @ApiOperation(value = "商品评论数量")
    @GetMapping(value = "/reply/config/{id}")
    public CommonResult<StoreProductReplayCountResponse> getReplyCount(@PathVariable Integer id) {
        return CommonResult.success(productService.getReplyCount(id));
    }

}



